<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Date Parsing [Universal Feed Parser]</title>
<link rel="stylesheet" href="feedparser.css" type="text/css">
<link rev="made" href="mailto:mark@diveintomark.org">
<meta name="generator" content="DocBook XSL Stylesheets V1.65.1">
<meta name="keywords" content="RSS, Atom, CDF, XML, feed, parser, Python">
<link rel="start" href="index.html" title="Documentation">
<link rel="up" href="advanced.html" title="Advanced Features">
<link rel="prev" href="advanced.html" title="Advanced Features">
<link rel="next" href="html-sanitization.html" title="HTML Sanitization">
</head>
<body id="feedparser-org" class="docs">
<div class="z" id="intro"><div class="sectionInner"><div class="sectionInner2">
<div class="s" id="pageHeader">
<h1><a href="/"><span>Universal Feed Parser</span></a></h1>
<p><span>Parse RSS and Atom feeds in Python.  3000 unit tests.  Open source.</span></p>
</div>
<div class="s" id="quickSummary"><ul>
<li class="li1">
<a href="http://sourceforge.net/projects/feedparser/"><span>Download</span></a> ·</li>
<li class="li2">
<a href="http://feedparser.org/docs/"><span>Documentation</span></a> ·</li>
<li class="li3">
<a href="http://feedparser.org/tests/"><span>Unit tests</span></a> ·</li>
<li class="li4"><a href="http://sourceforge.net/tracker/?func=browse&amp;group_id=112328&amp;atid=661937"><span>Report a bug</span></a></li>
</ul></div>
</div></div></div>
<div id="main"><div id="mainInner">
<p id="breadcrumb">You are here: <a href="index.html">Documentation</a> → <a href="advanced.html">Advanced Features</a> → <span class="thispage">Date Parsing</span></p>
<div class="section" lang="en">
<div class="titlepage">
<div>
<div><h2 class="title">
<a name="advanced.date" class="skip" href="#advanced.date" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Date Parsing</h2></div>
<div><div class="abstract">
<h3 class="title"></h3>
<p>Different feed types and versions use wildly different date formats.  <span class="application">Universal Feed Parser</span> will attempt to auto-detect the date format used in any date element, and parse it into a standard <span class="application">Python</span> 9-tuple, as documented in <a href="http://docs.python.org/lib/module-time.html">the <span class="application">Python</span> <tt class="filename">time</tt> module</a>.</p>
</div></div>
</div>
<div></div>
</div>
<p>The following elements are parsed as dates:</p>
<div class="itemizedlist"><ul>
<li>
<a href="reference-feed-updated.html" title="feed.updated">feed.updated</a> is parsed into <a href="reference-feed-updated_parsed.html" title="feed.updated_parsed">feed.updated_parsed</a>.</li>
<li>
<a href="reference-entry-published.html" title="entries[i].published">entries[i].published</a> is parsed into <a href="reference-entry-published_parsed.html" title="entries[i].published_parsed">entries[i].published_parsed</a>.</li>
<li>
<a href="reference-entry-updated.html" title="entries[i].updated">entries[i].updated</a> is parsed into <a href="reference-entry-updated_parsed.html" title="entries[i].updated_parsed">entries[i].updated_parsed</a>.</li>
<li>
<a href="reference-entry-created.html" title="entries[i].created">entries[i].created</a> is parsed into <a href="reference-entry-created_parsed.html" title="entries[i].created_parsed">entries[i].created_parsed</a>.</li>
<li>
<a href="reference-entry-expired.html" title="entries[i].expired">entries[i].expired</a> is parsed into <a href="reference-entry-expired_parsed.html" title="entries[i].expired_parsed">entries[i].expired_parsed</a>.</li>
</ul></div>
<div class="section" lang="en">
<div class="titlepage">
<div><div><h3 class="title">
<a name="advanced.date.history" class="skip" href="#advanced.date.history" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> History of Date Formats</h3></div></div>
<div></div>
</div>
<p>Here is a brief history of feed date formats:</p>
<div class="itemizedlist"><ul>
<li>
<acronym title="Channel Definition Format">CDF</acronym> states that all date values must conform to <acronym>ISO</acronym> 8601:1988.  <acronym>ISO</acronym> 8601:1988 is not a freely available specification, but a brief (non-normative) description of the date formats it describes is available here: <a href="http://hydracen.com/dx/iso8601.htm">ISO 8601:1988 Date/Time Representations</a>.</li>
<li>
<acronym title="Rich Site Summary">RSS</acronym> 0.90 has no date elements.</li>
<li>Netscape <acronym title="Rich Site Summary">RSS</acronym> 0.91 does not specify a date format, but examples within the specification show <acronym title="Request For Comments">RFC</acronym> 822-style dates with 4-digit years.</li>
<li>Userland <acronym title="Rich Site Summary">RSS</acronym> 0.91 states, “<span class="quote">All date-times in <acronym title="Rich Site Summary">RSS</acronym> conform to the Date and Time Specification of <acronym title="Request For Comments">RFC</acronym> 822.</span>”  <a href="http://www.ietf.org/rfc/rfc822.txt"><acronym title="Request For Comments">RFC</acronym> 822</a> mandates 2-digit years; it does not allow 4-digit years.</li>
<li>
<acronym title="Rich Site Summary">RSS</acronym> 1.0 states that all date elements must conform to <a href="http://www.w3.org/TR/NOTE-datetime"><acronym title="W3C Date Time Format">W3CDTF</acronym></a>, which is a profile of <acronym>ISO</acronym> 8601:1988.</li>
<li>
<acronym title="Rich Site Summary">RSS</acronym> 2.0 states, “<span class="quote">All date-times in <acronym title="Rich Site Summary">RSS</acronym> conform to the Date and Time Specification of RFC 822, with the exception that the year may be expressed with two characters or four characters (four preferred).</span>”</li>
<li>Atom 0.3 states that all date elements must conform to <a href="http://www.w3.org/TR/NOTE-datetime"><acronym title="W3C Date Time Format">W3CDTF</acronym></a>.</li>
<li>Atom 1.0 states that all date elements "MUST conform to the date-time production in <a href="http://www.ietf.org/rfc/rfc3339.txt"><acronym title="Request For Comments">RFC</acronym> 3339</a>.  In addition, an uppercase <tt class="literal">T</tt> character MUST be used to separate date and time, and an uppercase <tt class="literal">Z</tt> character MUST be present in the absence of a numeric time zone offset."</li>
</ul></div>
</div>
<div class="section" lang="en">
<div class="titlepage">
<div><div><h3 class="title">
<a name="advanced.date.formats" class="skip" href="#advanced.date.formats" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Recognized Date Formats</h3></div></div>
<div></div>
</div>
<p>Here is a representative list of the formats that <span class="application">Universal Feed Parser</span> can recognize in any date element:</p>
<div class="table">
<a name="id4954148" class="skip" href="#id4954148" title="link to this table"><img src="images/permalink.gif" alt="[link]" title="link to this table" width="8" height="9"></a> <h3 class="title">Recognized Date Formats</h3>
<table summary="Recognized Date Formats" border="1">
<colgroup>
<col>
<col>
<col>
</colgroup>
<thead><tr>
<th>Description</th>
<th>Example</th>
<th>Parsed Value</th>
</tr></thead>
<tbody>
<tr>
<td>valid RFC 822 (2-digit year)</td>
<td><tt class="literal">Thu, 01 Jan 04 19:48:21 GMT</tt></td>
<td><tt class="literal">(2004, 1, 1, 19, 48, 21, 3, 1, 0)</tt></td>
</tr>
<tr>
<td>valid RFC 822 (4-digit year)</td>
<td><tt class="literal">Thu, 01 Jan 2004 19:48:21 GMT</tt></td>
<td><tt class="literal">(2004, 1, 1, 19, 48, 21, 3, 1, 0)</tt></td>
</tr>
<tr>
<td>invalid RFC 822 (no time)</td>
<td><tt class="literal">01 Jan 2004</tt></td>
<td><tt class="literal">(2004, 1, 1, 0, 0, 0, 3, 1, 0)</tt></td>
</tr>
<tr>
<td>invalid RFC 822 (no seconds)</td>
<td><tt class="literal">01 Jan 2004 00:00 GMT</tt></td>
<td><tt class="literal">(2004, 1, 1, 0, 0, 0, 3, 1, 0)</tt></td>
</tr>
<tr>
<td>valid W3CDTF (numeric timezone)</td>
<td><tt class="literal">2003-12-31T10:14:55-08:00</tt></td>
<td><tt class="literal">(2003, 12, 31, 18, 14, 55, 2, 365, 0)</tt></td>
</tr>
<tr>
<td>valid W3CDTF (UTC timezone)</td>
<td><tt class="literal">2003-12-31T10:14:55Z</tt></td>
<td><tt class="literal">(2003, 12, 31, 10, 14, 55, 2, 365, 0)</tt></td>
</tr>
<tr>
<td>valid W3CDTF (yyyy)</td>
<td><tt class="literal">2003</tt></td>
<td><tt class="literal">(2003, 1, 1, 0, 0, 0, 2, 1, 0)</tt></td>
</tr>
<tr>
<td>valid W3CDTF (yyyy-mm)</td>
<td><tt class="literal">2003-12</tt></td>
<td><tt class="literal">(2003, 12, 1, 0, 0, 0, 0, 335, 0)</tt></td>
</tr>
<tr>
<td>valid W3CDTF (yyyy-mm-dd)</td>
<td><tt class="literal">2003-12-31</tt></td>
<td><tt class="literal">(2003, 12, 31, 0, 0, 0, 2, 365, 0)</tt></td>
</tr>
<tr>
<td>valid ISO 8601 (yyyymmdd)</td>
<td><tt class="literal">20031231</tt></td>
<td><tt class="literal">(2003, 12, 31, 0, 0, 0, 2, 365, 0)</tt></td>
</tr>
<tr>
<td>valid ISO 8601 (-yy-mm)</td>
<td><tt class="literal">-03-12</tt></td>
<td><tt class="literal">(2003, 12, 1, 0, 0, 0, 0, 335, 0)</tt></td>
</tr>
<tr>
<td>valid ISO 8601 (-yymm)</td>
<td><tt class="literal">-0312</tt></td>
<td><tt class="literal">(2003, 12, 1, 0, 0, 0, 0, 335, 0)</tt></td>
</tr>
<tr>
<td>valid ISO 8601 (-yy-mm-dd)</td>
<td><tt class="literal">-03-12-31</tt></td>
<td><tt class="literal">(2003, 12, 31, 0, 0, 0, 2, 365, 0)</tt></td>
</tr>
<tr>
<td>valid ISO 8601 (yymmdd)</td>
<td><tt class="literal">031231</tt></td>
<td><tt class="literal">(2003, 12, 31, 0, 0, 0, 2, 365, 0)</tt></td>
</tr>
<tr>
<td>valid ISO 8601 (yyyy-o)</td>
<td><tt class="literal">2003-335</tt></td>
<td><tt class="literal">(2003, 12, 1, 0, 0, 0, 0, 335, 0)</tt></td>
</tr>
<tr>
<td>valid ISO 8601 (yyo)</td>
<td><tt class="literal">03335</tt></td>
<td><tt class="literal">(2003, 12, 1, 0, 0, 0, 0, 335, 0)</tt></td>
</tr>
<tr>
<td>valid asctime</td>
<td><tt class="literal">Sun Jan  4 16:29:06 PST 2004</tt></td>
<td><tt class="literal">(2004, 1, 5, 0, 29, 6, 0, 5, 0)</tt></td>
</tr>
<tr>
<td>bogus RFC 822 (invalid day/month)</td>
<td><tt class="literal">Thu, 31 Jun 2004 19:48:21 GMT</tt></td>
<td><tt class="literal">(2004, 7, 1, 19, 48, 21, 3, 183, 0)</tt></td>
</tr>
<tr>
<td>bogus RFC 822 (invalid month)</td>
<td><tt class="literal">Mon, 26 January 2004 16:31:00 EST</tt></td>
<td><tt class="literal">(2004, 1, 26, 21, 31, 0, 0, 26, 0)</tt></td>
</tr>
<tr>
<td>bogus RFC 822 (invalid timezone)</td>
<td><tt class="literal">Mon, 26 Jan 2004 16:31:00 ET</tt></td>
<td><tt class="literal">(2004, 1, 26, 21, 31, 0, 0, 26, 0)</tt></td>
</tr>
<tr>
<td>bogus W3CDTF (invalid hour)</td>
<td><tt class="literal">2003-12-31T25:14:55Z</tt></td>
<td><tt class="literal">(2004, 1, 1, 1, 14, 55, 3, 1, 0)</tt></td>
</tr>
<tr>
<td>bogus W3CDTF (invalid minute)</td>
<td><tt class="literal">2003-12-31T10:61:55Z</tt></td>
<td><tt class="literal">(2003, 12, 31, 11, 1, 55, 2, 365, 0)</tt></td>
</tr>
<tr>
<td>bogus W3CDTF (invalid second)</td>
<td><tt class="literal">2003-12-31T10:14:61Z</tt></td>
<td><tt class="literal">(2003, 12, 31, 10, 15, 1, 2, 365, 0)</tt></td>
</tr>
<tr>
<td>bogus (MSSQL)</td>
<td><tt class="literal">2004-07-08 23:56:58.0</tt></td>
<td><tt class="literal">(2004, 7, 8, 14, 56, 58, 3, 190, 0)</tt></td>
</tr>
<tr>
<td>bogus (MSSQL-ish, without fractional second)</td>
<td><tt class="literal">2004-07-08 23:56:58</tt></td>
<td><tt class="literal">(2004, 7, 8, 14, 56, 58, 3, 190, 0)</tt></td>
</tr>
<tr>
<td>bogus (Korean)</td>
<td><tt class="literal">2004-05-25 오 11:23:17</tt></td>
<td><tt class="literal">(2004, 5, 25, 14, 23, 17, 1, 146, 0)</tt></td>
</tr>
<tr>
<td>bogus (Greek)</td>
<td><tt class="literal">Κυρ, 11 Ιούλ 2004 12:00:00 EST</tt></td>
<td><tt class="literal">(2004, 7, 11, 17, 0, 0, 6, 193, 0)</tt></td>
</tr>
<tr>
<td>bogus (Hungarian)</td>
<td><tt class="literal">július-13T9:15-05:00</tt></td>
<td><tt class="literal">(2004, 7, 13, 14, 15, 0, 1, 195, 0)</tt></td>
</tr>
</tbody>
</table>
</div>
<p><span class="application">Universal Feed Parser</span> recognizes all character-based timezone abbreviations defined in <acronym title="Request For Comments">RFC</acronym> 822.  In addition, <span class="application">Universal Feed Parser</span> recognizes the following invalid timezones:</p>
<div class="itemizedlist"><ul>
<li>
<tt class="constant">AT</tt> is treated as <tt class="constant">AST</tt>
</li>
<li>
<tt class="constant">ET</tt> is treated as <tt class="constant">EST</tt>
</li>
<li>
<tt class="constant">CT</tt> is treated as <tt class="constant">CST</tt>
</li>
<li>
<tt class="constant">MT</tt> is treated as <tt class="constant">MST</tt>
</li>
<li>
<tt class="constant">PT</tt> is treated as <tt class="constant">PST</tt>
</li>
</ul></div>
</div>
<div class="section" lang="en">
<div class="titlepage">
<div><div><h3 class="title">
<a name="advanced.date.register" class="skip" href="#advanced.date.register" title="link to this section"><img src="images/permalink.gif" alt="[link]" title="link to this section" width="8" height="9"></a> Supporting Additional Date Formats</h3></div></div>
<div></div>
</div>
<p><span class="application">Universal Feed Parser</span> supports many different date formats, but there are probably many more in the wild that are still unsupported.  If you find other date formats, you can support them by registering them with <tt class="function">registerDateHandler</tt>.  It takes a single argument, a callback function.  The callback function should take a single argument, a string, and return a single value, a 9-tuple <span class="application">Python</span> date in UTC.</p>
<div class="example">
<a name="example.registerdatehandler" class="skip" href="#example.registerdatehandler" title="link to this example"><img src="images/permalink.gif" alt="[link]" title="link to this example" width="8" height="9"></a> <h3 class="title">Example: Registering a third-party date handler</h3>
<pre class="programlisting python"><font color='navy'><b>import</b></font> feedparser
<font color='navy'><b>import</b></font> re

_my_date_pattern = re.compile( \
    r<font color='olive'>'(\d{,2})/(\d{,2})/(\d{4}) (\d{,2}):(\d{2}):(\d{2})'</font>)

<font color='navy'><b>def</b></font><font color='blue'><b> myDateHandler</b></font>(aDateString):
    <font color='olive'>"""parse a UTC date in MM/DD/YYYY HH:MM:SS format"""</font>
    month, day, year, hour, minute, second = \
        _my_date_pattern.search(aDateString).groups()
    <font color='navy'><b>return</b></font> (int(year), int(month), int(day), \
            int(hour), int(minute), int(second), 0, 0, 0)

feedparser.registerDateHandler(myDateHandler)
d = feedparser.parse(...)</pre>
</div>
<p>Your newly-registered date handler will be tried before all the other date handlers built into <span class="application">Universal Feed Parser</span>.  (More specifically, all date handlers are tried in "last in, first out" order; i.e. the last handler to be registered is the first one tried, and so on in reverse order of registration.)</p>
<p>If your date handler returns <tt class="constant">None</tt>, or anything other than a <span class="application">Python</span> 9-tuple date, or raises an exception of any kind, the error will be silently ignored and the other registered date handlers will be tried in order.  If no date handlers succeed, then the date is not parsed, and the <tt class="sgmltag-element">*_parsed</tt> value will not be present in the results dictionary.  The original date string will still be available in the appropriate element in the results dictionary.</p>
<a name="id4954950"></a><table class="tip" border="0" summary="">
<tr><td rowspan="2" align="center" valign="top" width="1%"><img src="images/tip.png" alt="Tip" title="" width="24" height="24"></td></tr>
<tr><td colspan="2" align="left" valign="top" width="99%">If you write a new date handler, you are encouraged (but not required) to <a href="http://sourceforge.net/projects/feedparser/">submit a patch</a> so it can be integrated into the next version of <span class="application">Universal Feed Parser</span>.</td></tr>
</table>
</div>
</div>
<div style="float: left">← <a class="NavigationArrow" href="advanced.html">Advanced Features</a>
</div>
<div style="text-align: right">
<a class="NavigationArrow" href="html-sanitization.html">HTML Sanitization</a> →</div>
<hr style="clear:both">
<div class="footer"><p class="copyright">Copyright © 2004, 2005, 2006 Mark Pilgrim</p></div>
</div></div>
</body>
</html>
